package cn.zjn.test.springboot.controller;

import cn.zjn.test.springboot.domain.Page;
import cn.zjn.test.springboot.domain.Plan;
import cn.zjn.test.springboot.annotationClass.PriInfoToken;
import cn.zjn.test.springboot.service.LoginService;
import cn.zjn.test.springboot.service.PResourcesService;
import cn.zjn.test.springboot.utils.JsonResult;
import cn.zjn.test.springboot.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: Zjn *Bring it on!!!*
 * @Date: 2021/2/1 13:06
 * Note:计划页资源
 */
@RestController
public class PlanResourcesController {
    @Autowired
    PResourcesService pResourcesService;

    @Autowired
    LoginService loginService;

    @PriInfoToken
    @GetMapping("/getPlan")
    public JsonResult getPlan(@RequestParam Integer amount, @RequestParam(defaultValue = "5") Integer limit){
        return new JsonResult(pResourcesService.getPlan(TokenUtil.getTokenUserId(),new Page(amount,limit)));
    }


//    @PriInfoToken
    @PostMapping("/addPlan")
    public JsonResult addPlan(@RequestBody Plan plan,@RequestParam Integer userid){
        String userName = loginService.findUserNameByUserId(userid);
        try {
            pResourcesService.addPlan(plan,userName);
        }catch (Exception e){
            System.out.println(e);
            return new JsonResult(Map.of("token",1),"500","添加计划失败");
        }
        return new JsonResult(Map.of("token",0),"200","添加计划成功");
    }
}
